package com.rabbit.work.contoller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.rabbit.work.constant.OrderSttsConstant;
import com.rabbit.work.po.Order;
import com.rabbit.work.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private IOrderService orderService;


    @RequestMapping("/order/{prodId}/{num}")
    public String saveOrder(@PathVariable("prodId") Long prodId,@PathVariable("num") Integer num,Model model) {
        Snowflake snowflake = IdUtil.getSnowflake(1L);
        Order order = new Order();
        order.setId(snowflake.nextId());
        order.setOrderNo(RandomUtil.randomNumbers(10));
        order.setUserId(1L);
        order.setProdId(prodId);
        order.setOrderStts(OrderSttsConstant.ORDER_STTS_CREATE);
        order.setBuyNum(num);
        orderService.saveOrder(order);
        model.addAttribute("orderNo", order.getOrderNo());
        return "success";
    }


    @RequestMapping("/order/list")
    public String showOrderList(Model model) {
        List<Order> orders = orderService.listOrders(1L);
        model.addAttribute("orderList", orders);
        return "order_list";
    }

}
